<%--
*
* 作者： Sky
*
* 描述： 功能授权
*
--%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<jsp:directive.page import="com.toncen.samepms.system.dao.FunctionPermissionDao"/>
<jsp:directive.page import="com.toncen.samepms.system.domain.FunctionPermission"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<title>功能授权</title>
		<%@include file="/share/header.jsp"%>
		<%@include file="/share/DhmlxTreeheader.jsp"%>
	</head>
	<%
		String subjectType=request.getParameter("subjectType");
		String subjectId=request.getParameter("subjectId");
		String resourceType=request.getParameter("resourceType");
		Class resourceClass=Class.forName(resourceType);
		String level=request.getParameter("level");		
		boolean checkbox=Boolean.parseBoolean(request.getParameter("checkbox"));
		boolean threeState=Boolean.parseBoolean(request.getParameter("threeState"));
		FunctionPermissionDao functionPermissionDao=new FunctionPermissionDao();
		FunctionPermission functionPermission=new FunctionPermission();
		functionPermission.setSubjectType(subjectType);
		functionPermission.setSubjectId(Long.parseLong(subjectId));
		functionPermission.setResourceType(resourceType);
		Set<Long> resourceIds=functionPermissionDao.getResourceFunctionPermissions(functionPermission);
	%>	
	<body scroll="no" style="margin:0;padding: 0;border: 0;">
		<div align="center"  style="margin:0;padding: 0;border: 0;">
			<span style="width: 100%;font-weight: bold;text-align:center;text-valign:middle;font-size: 20px;">功能授权</span>
			<p  style="margin:5px;">
				<a href="javascript:tree.openAllItems(0);">全部展开</a> 
				| 
				<a href="javascript: tree.closeAllItems(0);">全部收缩</a>
				<br>
			</p>
		</div>
		<div id="treeboxbox_tree" align="left" style="height: 100%;"></div>
		<script> 
            tree=new dhtmlXTreeObject("treeboxbox_tree","100%","100%",0);//创建树对象
            tree.setImagePath("<%=path%>/dhtmlxtree/imgs/csh_bluebooks/");//设置树图标路径
            tree.setXMLAutoLoading("<%=path%>/share/treeData.jsp?type=<%=resourceClass.getSimpleName()%>&level=1"); //设置数据路径
            tree.setDataMode("json");//设置数据类型
            tree.enableCheckBoxes(<%=checkbox%>);//启用checkbox
            tree.enableThreeStateCheckboxes(<%=threeState%>);//启用三态checkbox
            tree.setOnClickHandler(onNodeSelect);//set function object to call on node select
            tree.loadJSON("<%=path%>/share/treeData.jsp?type=<%=resourceClass.getSimpleName()%>&level=1");//加载第一级节点
            tree.attachEvent("onXLE",setCheckedValue);//加载完树后选中
            document.getElementById("treeboxbox_tree").style.overflowX="hidden";//设置滚动条
            document.getElementById("treeboxbox_tree").style.overflowY="auto";//设置滚动条
           	/*
             *设置checked的值为选中状态
            */                      
            function setCheckedValue(){ 
               <%for(Long resourceId:resourceIds){%>
				tree.setCheck("<%=resourceId%>",true);	
			   <%}%>
 			   $(document.body).height($(frameElement).height()-12-12); 
            }  
			//选中执行的事件           
            function onNodeSelect(nodeId){
               //alert(tree.getOpenState(nodeId));
               if(tree.getOpenState(nodeId)==1){
               		tree.closeItem(nodeId);
               }else{
               		tree.openItem(nodeId);
               }
            }                        
            //授权
            function onDoPrivilege(parameters){
           			 top.MyWindow.confirm({
							content : '确定保存已分配的功能权限吗？',
							title : '确认框',
							container : top,
							callback :function(action){
											if(action == 'ok'){
								             	var resourceIds=new Array();
												var allResourceIds=tree.getAllChecked().split(",");
												for(var i=0;i<allResourceIds.length;i++){
													resourceType = tree.getUserData(allResourceIds[i],"type");//获取subjectType
													leaf = tree.getUserData(allResourceIds[i],"leaf");//获取leaf
													//alert(resourceType+":"+leaf);
													if(resourceType=="<%=resourceType%>"&&leaf){
														resourceIds[resourceIds.length]=allResourceIds[i];
													}
												}
												//alert(resourceIds);
												//使用ajax进行操作，以前追加参数串方式参数过多会截取
												$.ajax({
												   type: "POST",
												   dataType: "html",
												   url: "permitResource.jsp",
												   data: {subjectType:'<%=subjectType%>',subjectId:'<%=subjectId%>',resourceType:'<%=resourceType%>',resourceIds:resourceIds},
												   success: function(data){
													   	top.MyWindow.tips({
															content : '授权成功!',
															title : '提示',
															type : 'success',
															container : top					
														});		
														top.MyWindow.close(parameters.id,parameters.container);	
												   },
												   error:function(data){
												   		top.MyWindow.tips({
															content : '授权失败!',
															title : '提示',
															type : 'error',
															container : top					
														});	
												   }
												 });				  
							 				 }
		 				  }
					});
            }
        </script>
	</body>
</html>
